<?php
class M_DbTable_Enquire extends Apps_Db_Table_Abstract {
	protected $_name = 'enquire';

	const STATUS_ACTIVE 	= 1;    //unchecked
	const STATUS_INACTIVE 	= 0;    //checked
	const STATUS_PAID 		= 2;    //payed

	const STATUS_DELETED    = -1;   //deleted

	const REFERENCE_TYPE_TOUR 		= 1;
	const REFERENCE_TYPE_HOTEL		= 2;
	const REFERENCE_TYPE_CRUISE		= 3;
	const REFERENCE_TYPE_NOT_DEFINE	= 0;

	public function getList() {
		$q = $this->_db
				  ->select()
				  ->from('enquire', null)
				  ->joinLeft('customer', 'customer.email = enquire.email', null)
				  ->columns(array(
				  			'id'					=> 'enquire.id',
				  			'reference_type'		=> 'enquire.reference_type',
				  			'reference_id'	        => 'enquire.reference_id',
				  			'enquire_status_id'		=> 'enquire.enquire_status_id',
				  			'full_name'				=> 'enquire.full_name',
					  		'email'					=> 'enquire.email',
					  		'phone'					=> 'enquire.phone',
					  		'country_residence'		=> 'enquire.country_residence',
							'subject'				=> 'enquire.subject',
					  		'message'				=> 'enquire.message',
					  		'status'				=> 'enquire.status',
					  		'description'			=> 'enquire.description',
				  			'customer_id' 			=> 'customer.id',
				  			'customer_name'			=> 'customer.full_name'
				  		))
				  ->order('enquire.status DESC')
				  ->order('id DESC');

		return $q;
	}

	public function getEnquireyById($enquireId) {
		$q = $this->_db
				  ->select()
				  ->from('enquire')
				  ->joinLeft('tour', 'enquire.reference_type = '. self::REFERENCE_TYPE_TOUR . ' AND tour.id = reference_id', null)
				  ->joinLeft('hotel', 'enquire.reference_type = '. self::REFERENCE_TYPE_HOTEL . ' AND hotel.id = reference_id', null)
				  ->where('enquire.id = ?', $enquireId);
		return $q;
	}
}